<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="参数模板" prop="templetParaId">
        <el-input v-model="queryParams.bookmark" placeholder="请输入参数模板" clearable
                  @keyup.enter.native="handleQuery" readonly @click.native="showSelect"/>
      </el-form-item>
      <el-form-item label="启用标志" prop="enabledFlag">
        <el-select v-model="queryParams.enabledFlag" placeholder="请输入启用标志" clearable>
          <el-option v-for="dict in dict.type.sys_yes_no" :key="dict.value" :label="dict.label"
                     :value="dict.value"/>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-dialog title="参数模板" :visible.sync="userSelectTableShow" @close="close" width="860px">
      <Lov :select="queryLov" v-if="selectTableShow" ref="refSelectTable"></Lov>
    </el-dialog>

    <LovPlus :openLovPara="openLovPara" ref="refSelectTable1"/>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini"
                   @click="handleAdd" v-hasPermi="['basic:docFileTmpParaLink:add']">新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single"
                   @click="handleUpdate" v-hasPermi="['basic:docFileTmpParaLink:edit']">修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple"
                   @click="handleDelete" v-hasPermi="['basic:docFileTmpParaLink:remove']">删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini"
                   @click="handleExport" v-hasPermi="['basic:docFileTmpParaLink:export']">导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="docFileTmpParaLinkList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="参数ID" align="center" prop="templetParaId"/>
      <el-table-column label="启用标志" align="center" prop="enabledFlag">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_yes_no" :value="scope.row.enabledFlag"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit"
                     @click="handleUpdate(scope.row)" v-hasPermi="['basic:docFileTmpParaLink:edit']">修改
          </el-button>
          <el-button size="mini" type="text" icon="el-icon-delete"
                     @click="handleDelete(scope.row)" v-hasPermi="['basic:docFileTmpParaLink:remove']">删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
                @pagination="getList"/>

    <!-- 添加或修改文件模板-参数集对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="参数ID" prop="templetParaId">
          <el-input v-model="form.templetParaId" placeholder="请输入参数ID"/>
        </el-form-item>
        <el-form-item label="启用标志">
          <el-radio-group v-model="form.enabledFlag">
            <el-radio v-for="dict in dict.type.sys_yes_no" :key="dict.value" :label="dict.value">{{ dict.label }}
            </el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listDocFileTmpParaLink,
  getDocFileTmpParaLink,
  delDocFileTmpParaLink,
  addDocFileTmpParaLink,
  updateDocFileTmpParaLink
} from "@/api/basic/docFileTmpParaLink";
import Lov from "@/components/Lov/index";
import LovPlus from "@/components/Lov/lovPlus";

export default {
  name: "DocFileTmpParaLink",
  dicts: ['sys_yes_no'],
  components: {Lov, LovPlus},
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 文件模板-参数集表格数据
      docFileTmpParaLinkList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        templetParaId: null,
        templetId: null,
        enabledFlag: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        templetParaId: [
          {required: true, message: "参数ID不能为空", trigger: "blur"}
        ],
        templetId: [
          {required: true, message: "模板ID不能为空", trigger: "blur"}
        ],
      },

      queryLov: {
        url: '/basic/docFileTempletPara/list',
        resultSize: 2,
        single: true,
        //确定的回调
        submit: (data) => {
          this.userSelectTableShow = false

          if (!Array.isArray(data)) {
            this.queryParams.templetParaId = data.templetParaId
            this.queryParams.bookmark = data.bookmark
          } else {
            this.queryParams.templetParaId = data[0].templetParaId
            this.queryParams.bookmark = data[0].bookmark
          }
        },
        //取消的回调
        cancel: () => {
          this.userSelectTableShow = false
        },
        columns: [
          {prop: 'templetParaId', label: 'ID', width: 80, key: true, show: true},
          {prop: 'bookmark', label: '书签代码', width: 200, search: true},
          {prop: 'bookmarkDescription', label: '书签描述', width: 200, search: true},
          {prop: 'fontFamily', label: '字体型号', width: 80}
        ]
      },
      userSelectTableShow: false,
      selectTableShow: false,
      openLovPara: {
        //弹出lov
        openLov:false,
        //lovWidth:700,
        lovTitle:"参数模板",
        queryUrl: '/basic/docFileTempletPara/list',
        res: {
          data: 'rows',
          total: 'total'
        },
        //默认查询参数
        /*defaultParam: {
          roleId: 2//用户
        },*/
        //多选可选这的条数，单选
        //resultSize: 2,
        //表示单选
        single: true,
        //确定的回调
        submit: (data) => {
          if (!Array.isArray(data)) {
            this.queryParams.templetParaId = data.templetParaId
            this.queryParams.bookmark = data.bookmark
          } else {
            this.queryParams.templetParaId = data[0].templetParaId
            this.queryParams.bookmark = data[0].bookmark
          }
          this.openLovPara.openLov = false;
        },
        //取消的回调
        cancel: () => {
          this.openLovPara.openLov = false;
        },
        columns: [
          {prop: 'templetParaId', label: 'ID', width: 80, key: true, show: true},
          {prop: 'bookmark', label: '书签代码', width: 200, search: true},
          {prop: 'bookmarkDescription', label: '书签描述', width: 200, search: true},
          {prop: 'fontFamily', label: '字体型号', width: 80}
        ]
      },
    };
  },
  props: {
    select: Object
  },
  watch: {
    select: {
      handler(newVal, oldVal) {
        this.queryParams.templetId = newVal.templetId;
        this.getList();
      },
      //深度监控对象里面的值
      deep: true,
      //首次通过created去执行immediate: false设置未false
      immediate: false
    }
  },
  created() {
    this.queryParams.templetId = this.select && this.select.templetId;
    this.getList();
  },
  methods: {
    close() {
      this.userSelectTableShow = false
    },
    showSelect() {
      this.openLovPara.openLov = true
      /*this.userSelectTableShow = true

      this.selectTableShow = false

      this.$nextTick(() => {
        this.selectTableShow = true
      })*/
    },
    /** 查询文件模板-参数集列表 */
    getList() {
      this.loading = true;
      listDocFileTmpParaLink(this.queryParams).then(response => {
        this.docFileTmpParaLinkList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        linkId: null,
        templetParaId: null,
        templetId: null,
        enabledFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.linkId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加文件模板-参数集";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const linkId = row.linkId || this.ids
      getDocFileTmpParaLink(linkId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改文件模板-参数集";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.linkId != null) {
            updateDocFileTmpParaLink(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addDocFileTmpParaLink(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const linkIds = row.linkId || this.ids;
      this.$modal.confirm('是否确认删除文件模板-参数集编号为"' + linkIds + '"的数据项？').then(function () {
        return delDocFileTmpParaLink(linkIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('basic/docFileTmpParaLink/export', {
        ...this.queryParams
      }, `docFileTmpParaLink_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
